package se.topshelf.golftracker.web.controller.teetime;


import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import se.topshelf.golftracker.core.domain.*;
import se.topshelf.golftracker.core.service.BaseService;
import se.topshelf.golftracker.core.service.PlayerService;
import se.topshelf.golftracker.web.controller.BaseEntityController;
import se.topshelf.golftracker.web.controller.player.GolfIdEditor;
import se.topshelf.golftracker.web.controller.player.PlayerValidator;
import se.topshelf.golftracker.web.editor.BaseEntityEditor;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SortedSet;
import java.util.TreeSet;

@Controller
@SessionAttributes("entity")
@RequestMapping("/teetime")
public class TeeTimeController extends BaseEntityController<TeeTime> {
    static final String ENTITY_URL_CONTEXT_PATH = "teetime";
    public static final String ALL_COURSES_MODEL_ATTRIBUTE = "allCourses";
    public static final String ALL_MAX_PARTICIPANTS_MODEL_ATTRIBUTE = "allMaxParticipants";
    public static final String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    @Resource  // Automatically set ("wired") by Spring
    private PlayerService playerService;

    public TeeTimeController() {
        super(TeeTime.class);
    }

    @Override
    protected String getEntityUrlContextPath() {
        return ENTITY_URL_CONTEXT_PATH;
    }

    @Override
    protected BaseService getBaseService() {
        return playerService;
    }

    @Override
    protected void addModelReferenceData(ModelMap model, TeeTime entity) {
        SortedSet<Integer> allMaxParticipants = new TreeSet<Integer>();
        allMaxParticipants.add(1);
        allMaxParticipants.add(2);
        allMaxParticipants.add(3);
        allMaxParticipants.add(4);
        allMaxParticipants.add(5);
        allMaxParticipants.add(6);
        allMaxParticipants.add(7);
        allMaxParticipants.add(8);
        model.put(ALL_MAX_PARTICIPANTS_MODEL_ATTRIBUTE, allMaxParticipants);
        model.put(ALL_COURSES_MODEL_ATTRIBUTE, super.fetchAllEntitiesAndBuildReferenceDataFor(Course.class));

    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        binder.registerCustomEditor(Date.class, "teeTime", new CustomDateEditor(new SimpleDateFormat(DATE_PATTERN), false));
        binder.registerCustomEditor(Course.class, "course", new BaseEntityEditor<Course>(Course.class, playerService));
    }

    @Override
    protected Validator getValidator() {
        return new TeeTimeValidator(playerService);
    }

    // For tests
    protected void setPlayerService(PlayerService playerService) {
        this.playerService = playerService;
    }
}
